﻿using Jinjia.Approve.Entity.Entity.JJFlow;
using Jinjia.Approve.IRepository.JJFlow;
using Jinjia.Core.BaseInfrastructure.StorageModel;
using Jinjia.Core.BaseInfrastructure.StorageModel.Impl;
using Jinjia.Core.UnitOfWork.UnitOfWorkComponent;
using System.Threading.Tasks;

namespace Jinjia.Approve.Repository.JJFlow
{
    public class FlowDesignRepository : BaseRepository<jj_flow_design>, IFlowDesignRepository
    {
        public FlowDesignRepository(IUnitOfWork unitOfWork, IInitUserInfo initUserInfo) : base(unitOfWork, initUserInfo, "")
        {
        }

        public async Task<jj_flow_design> GetByTemplateId(int template_id)
        {
            return await _db.Queryable<jj_flow_design>().FirstAsync(x => x.template_id == template_id && x.deleted == 0);
        }

        public async Task<int> Save(jj_flow_design data)
        {
            if (data.id <= 0) //新增
            {
                var id = await _db.Insertable(data).ExecuteReturnIdentityAsync();
                return id;
            }
            else
            {
                await _db.Updateable(data)
                    .Where(x => x.id == data.id)
                    .ExecuteCommandAsync();
                return data.id;
            }
        }
    }
}